package com.jackchong.help.touch;

import android.annotation.SuppressLint;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewConfiguration;

import com.jackchong.base.BaseApplication;

/**
 * author : chong.huang
* create : 2021/6/7
 * desc   :
 */
@SuppressLint("ClickableViewAccessibility")
public class OnClickTouchListener implements View.OnTouchListener {

    private float mDownX, mDownY;
    private final int mTouchSlop = ViewConfiguration.get(BaseApplication.getInstance()).getScaledTouchSlop();
    private final View.OnClickListener mOnClickListener;

    public OnClickTouchListener(View.OnClickListener clickListener) {
        mOnClickListener = clickListener;
    }

    @SuppressLint("ClickableViewAccessibility")
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        float x = v.getX();
        float y = v.getY();

        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                mDownX = x;
                mDownY = y;
                break;
            case MotionEvent.ACTION_UP:
                if (Math.abs(mDownX - x) <= mTouchSlop && Math.abs(mDownY - y) <= mTouchSlop && mOnClickListener != null) {
                    mOnClickListener.onClick(v);
                }
                break;
        }
        return false;
    }
}
